package com.scuec.tourismwebsite.controller;

import com.github.pagehelper.PageInfo;
import com.scuec.tourismwebsite.entity.Post;
import com.scuec.tourismwebsite.service.PostService;
import com.scuec.tourismwebsite.vo.UserPost;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("tour/v1")
public class PostController {
    @Autowired
    private PostService postService;

    @GetMapping("postpages")
    public PageInfo<UserPost> findUserPostPages(Integer pageNum, Integer pageSize) {
        return postService.findUserPostPages(pageNum, pageSize);
    }

    @GetMapping("postpage")
    public PageInfo<UserPost> findUserPostPage(String nickname, Integer pageNum, Integer pageSize) {
        return postService.findUserPostPage(nickname, pageNum, pageSize);
    }

    @GetMapping("post")
    public    List<UserPost> findPostAll(){
        return postService.findPostAll();
    };

    @DeleteMapping("post/{post_id}")
    public Integer deletePostById(@PathVariable("post_id") int post_id) {
        return postService.deletePostById(post_id);
    }

    @GetMapping("post/{post_id}")
    public UserPost findPostById(@PathVariable("post_id") Integer post_id){
        return postService.findPostById(post_id);
    }

    @PutMapping("post/{post_id}/{likes_count}/{comments_count}/{collect_counts}")
    public Integer updatePostCount(@PathVariable("post_id") Integer post_id,@PathVariable("likes_count")Integer likes_count,@PathVariable("comments_count") Integer comments_count,@PathVariable("collect_counts") Integer collect_counts){
        return postService.updatePostCount(post_id, likes_count, comments_count, collect_counts);
    }

    @GetMapping("userPost")
    public  List<UserPost> findUserPostByCategory(@RequestParam() String category){
        return postService.findUserPostByCategory(category);
    }

    @PostMapping("post")
    public Integer addPost(@RequestBody Post post){
        return postService.insertPost(post);
    }
}